<?php

class acl extends main {
    function __construct(){
        parent::__construct();
        $this->loadModel('g5_admin_acl');
        $this->loadModel('g5_admin_group');
        $this->loadModel('g5_admin_acl_group');
    }
    function index(){
        $data  = $this->g5_admin_acl->getAll();
        $tree = new Tree($data);
        $tree->set_array_colum(array("id"=>"acl_id","parent_id"=>"top_id","name"=>"acl_name"));
        $this->aclTree      = $tree->get_tree(0,"\$spacer\$acl_name");
        $this->formAction = BASEURI.'acl/add';
        $this->is_validator = true;
        $this->adminlog($this->systemLang['watchResourceList']);
    }

    function add(){
        if( ! empty($_POST['acl']) ){
            $acl = $_POST['acl'];
            if($insert = $this->g5_admin_acl->insert($acl)){
                $this->adminlog($this->systemLang['addResourceSuccess'].$insert);
                $this->report($this->systemLang['addResourceSuccess'], BASEURI.'acl/index');
            }
        }
        $this->report($this->systemLang['addResourceFailed'], BASEURI.'acl/index');
    }

    function delete(){
        $ids  = isset($_GET[2]) ? array($_GET[2]) : $_POST['select_id'];
        if($this->g5_admin_acl->in(array('acl_id'=>$ids))->delete()){
            $this->g5_admin_acl->makecache();
            $this->adminlog($this->systemLang['delResourceSuccess'].implode(',',$ids));
            $this->report($this->systemLang['delResourceSuccess'], BASEURI.'acl/index');
        }else{
            $this->report($this->systemLang['delResourceFailed'], BASEURI.'acl/index');
        }

    }

    function edit(){
        $acl_id  = isset($_GET[2]) ? $_GET[2] : 0;
        if( ! empty($_POST)){
            $acl    =    $_POST['acl'];
            $acl_id    =    $_POST['acl_id'];
            if($this->g5_admin_acl->update($acl, array('acl_id'=>$acl_id))){
                    $this->g5_admin_acl->makecache();
                     $this->adminlog($this->systemLang['updateResourceSuccess']. $acl_id);
                     $this->report($this->systemLang['updateResourceSuccess'], BASEURI.'acl/index');
            }else{
                    $this->report($this->systemLang['updateResourceFailed']. $acl_id, BASEURI.'acl/index');
            }
        }else{
            $acl    = $this->g5_admin_acl->getAll('*', array('acl_id!='=>$acl_id));
            $tree   = new Tree($acl);
            $tree->set_array_colum(array("id"=>"acl_id","parent_id"=>"top_id","name"=>"acl_name"));
            $this->aclTree    =    $tree->get_tree(0,"\$spacer\$acl_name");
            $this->aclData    =    $this->g5_admin_acl->getOne('*', array('acl_id'=>$acl_id));
            $this->formAction    =    BASEURI."acl/edit";
        }
    }

    function privilege(){
        $group_id  = isset($_GET[2]) ? $_GET[2] : 0;
        if( ! empty($_POST)){
            $groups =    $_POST['group_id'];
            $acls   =    $_POST['acl_id'];
            $result = $this->g5_admin_acl_group->in(array('group_id'=>$groups))->delete();
            //插入新关系
            foreach ($groups as $group_id){
                foreach ($acls as $acl_id){
                    $this->g5_admin_acl_group->replace(array('acl_id'=>$acl_id,'group_id'=>$group_id));
                }
            }
            //缓存资源
            $this->g5_admin_acl->makecache();
            $this->adminlog($this->systemLang['userAuthorizedSuccess'].implode(',', $groups));
            $this->report($this->systemLang['userAuthorizedSuccess'], BASEURI.'acl/privilege/'.$groups[0]);
        }else{
            $all_acl = $this->g5_admin_acl_group->getAll('acl_id', array('group_id'=>$group_id));
            $myacl  = array();
            foreach ($all_acl as $row){
                $myacl[] = $row['acl_id'];
            }

            // 获取子权限
            $acls            = $this->g5_admin_acl->getAll('*', array('top_id'=>0));
            foreach ($acls as $row){
                $acl_child[$row['acl_id']]    =    $this->g5_admin_acl->getAll('*', array('top_id'=>$row['acl_id']));
                foreach ($acl_child[$row['acl_id']] as $m){
                    $acl_child[$m['acl_id']] = $this->g5_admin_acl->getAll('*', array('top_id'=>$m['acl_id']));
                    foreach ($acl_child[$m['acl_id']] as $n){
                        $acl_child[$n['acl_id']] = $this->g5_admin_acl->getAll('*', array('top_id'=>$n['acl_id']));
                    }
                }
            }

            $userGroup       = $this->g5_admin_group->getAll('group_id,group_name,group_value'); // 获取用户组
            $this->userGroup = $userGroup;
            $this->acl          = $acls;
            $this->acl_child  = $acl_child;
            $this->myacl      = $myacl;
            $this->group_id     = $group_id;
            $this->formAction    = BASEURI."acl/privilege";
        }
    }



}